#include "ProgressBar.h"

ProgressBar::ProgressBar(int width) : barWidth(width), lastPos(-1) {
}

ProgressBar::~ProgressBar() {
}

void ProgressBar::clearLine() {
    std::cout << "\r";
    for (int i = 0; i < 120; i++) {
        std::cout << " ";
    }
}

void ProgressBar::update(float progress, int current, int total, const std::string& currentFile) {
    int pos = barWidth * progress;
    
    // 如果位置没有变化且不是首次显示，不更新显示（优化输出）
    if (pos == lastPos && lastPos != -1) {
        return;
    }
    
    lastPos = pos;
    
    // 先清除整行
    clearLine();
    
    // 重新显示进度条和当前处理的文件
    std::cout << "\r[";
    for (int i = 0; i < barWidth; ++i) {
        if (i < pos) std::cout << "=";
        else if (i == pos) std::cout << ">";
        else std::cout << " ";
    }
    std::cout << "] " << int(progress * 100.0) << "% " << current << "/" << total 
              << " 正在处理: " << currentFile;
    std::cout.flush();
}

void ProgressBar::complete(int total) {
    // 完成进度条
    clearLine();
    std::cout << "\r[";
    for (int i = 0; i < barWidth; ++i) std::cout << "=";
    std::cout << "] 100% " << total << "/" << total << " 处理完成!" << std::endl;
} 